package cn.jdy.plugins.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.plugins.entity.Attachment;
import cn.jdy.plugins.entity.Company;
import cn.jdy.plugins.entity.CompanyContext;
import cn.jdy.plugins.entity.Info;
import cn.jdy.plugins.service.AttachmentService;
import cn.jdy.plugins.service.CompanyContextService;
import cn.jdy.plugins.service.CompanyService;
import cn.jdy.plugins.service.InfoService;

/**
 * 企业内容展示 控制类
 *
 * @author zhaigt
 */
@Controller
@RequestMapping("/companyContext")
public class CompanyContextCtrl extends BaseCtrl {

	@Autowired
	private CompanyContextService companyContextService;
	@Autowired
	private AttachmentService attachmentService;
	@Autowired
	private CompanyService companyService;
	@Autowired
	private InfoService infoService;
	
	
	/**
	 * 初始化
	 */
	@RequestMapping("/list")
	public ModelAndView list() {
		return new ModelAndView("/company/companyContextList.jsp");
	}
	/**
	 * 企业内容展示 审核列表<br>
	 * 包含列表查询以及分页功能
	 *
	 * @return
	 */
	@RequestMapping("/findbrands")
	@ResponseBody
	public Map<String, Object> findbrands(CompanyContext bo) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			jsonMap.put("dataList", companyContextService.queryList(bo));
			Long total = companyContextService.count();
			jsonMap.put("iTotalDisplayRecords", total);
			jsonMap.put("iTotalRecords", total);
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "findbrands", e);
		}
		return jsonMap;
	}
	

	/**
	 * 企业内容编辑
	 *
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable("id") Long id) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/company/companyContext.jsp");
		CompanyContext context = companyContextService.find(id);
		List<Attachment> attchs = attachmentService.findAttachmentByBrandId(context.getId());
		mv.addObject("context", context);
		mv.addObject("attchments", attchs);
		return mv;
	}

	/**
	 * 审核内容
	 *
	 * @param brandId
	 * @param status
	 * @return
	 */
	@RequestMapping("update")
	@ResponseBody
	public Map<String, Object> update(Integer companyId,String brandId, String status,String advice) {
		Map<String, Object> map = new HashMap<String, Object>();
		int backInfo = 0;
		backInfo = companyContextService.updStatus(brandId, status,advice);
		map.put("message", backInfo);
		
		Company company = companyService.findOne("id", companyId); 
		if(company!=null){
			Info info = new Info();
			info.setType("1");
			if("1".equals(status)){
				info.setTitle("企业申请展示审核通过通知");
				info.setContext("您申请的企业展示已通过审核       <a target='_blank' href='http://127.0.0.1:8080/companyContext/index/"+companyId+"' >查看 </a>");
			}else{
				info.setTitle("企业申请展示审核不通过通知");
				info.setContext("您申请的企业展示未通过审核原因：  "+advice+"     <a target='_blank' href='http://127.0.0.1:8080/companyContext/rediradd' >重新申请 </a>");
			}
			// 公司的联系方式
			info.setUserid(memberService.getMobile());
			infoService.saveOrUpd(info);
		}
		return map;
	}

	/**
	 * 保存
	 * @param entity
	 * @return
	 */
	@RequestMapping("save")
	@ResponseBody
	public ModelAndView save(CompanyContext entity) {
		try {
			companyContextService.updateWithValidator(entity);
		} catch(IllegalAccessException e) {
			BaseLog.e(this.getClass(), "save", e);
		} catch(NoSuchFieldException e) {
			BaseLog.e(this.getClass(), "save", e);
		}
		return new ModelAndView("redirect:/companyContext/list");
	}

	/**
	 * 删除推荐
	 *
	 * @return
	 */
	@RequestMapping("/delete/{id}")
	@ResponseBody
	public Map<String, Object> delete(@PathVariable("id") Long id) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
		  companyContextService.delete(id);
    } catch (Exception e) {
      map.put("statusCode", 500);
    }
		map.put("statusCode", 200);
		return map;
	}
}
